﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RattlerRace
{
    public class ScoreTextHandler
    {
        private static List<Points> _hallOfFame;
        private static List<Points> _today;
        private static DateTime _dateToday;

        public static List<Points> HallOfFame { get { return _hallOfFame; } set { _hallOfFame = value; } }
        public static List<Points> Today { get { return _today; } set { _today = value; } }
        public static DateTime DateToday { get { return _dateToday; } set { _dateToday = value; } }

        public ScoreTextHandler()
        {
            _hallOfFame = new List<Points>();
            _today = new List<Points>();
            _dateToday = new DateTime();
            _dateToday = DateTime.Now;
        }

        public bool checkDates(String d)
        {
            bool good = false;
            String[] datum = new String[3];
            datum = d.Split('.');
            if (Convert.ToInt32(datum[0]) == _dateToday.Day && Convert.ToInt32(datum[1]) == _dateToday.Month && Convert.ToInt32(datum[2]) == _dateToday.Year)
            {
                return true;
            }
            return good;
        }

        public static bool reachedHighScore(int tocke)
        { //Vrne true, če je player dosegel highscore 
            bool highscore = false;
            foreach (Points p in _hallOfFame)
            {
                if (p.CurrentPoints < tocke)
                {
                    return true;
                }
            }
            foreach (Points p in _today)
            {
                if (p.CurrentPoints < tocke)
                {
                    return true;
                }
            }
            return highscore;
        }

        public void addNewScore(Points novo)
        {
            for (int i = 0; i < 5; i++) 
            {
                if (_hallOfFame[i].CurrentPoints <= novo.CurrentPoints)
                {
                    _hallOfFame.Insert(i, novo);
                    break;
                }
            }
            for (int i = 0; i < 5; i++) 
            {
                if (_today[i].CurrentPoints <= novo.CurrentPoints)
                {
                    _today.Insert(i, novo);
                    break;
                }
            }
            if (_today.Count > 5)
            {
                _today.RemoveAt(5);
            }
            if (_hallOfFame.Count > 5)
            {
                _hallOfFame.RemoveAt(5);
            }

            saveScores();
        }

        public void saveScores()
        {
            StreamWriter sw = new StreamWriter("../../hallOfFamePoints.txt", false);
            String line;
            foreach (Points p in _hallOfFame)
            {
                line = p.CurrentPoints.ToString() + "," + p.Player;
                sw.WriteLine(line);
            }
            sw.Close();

            sw = new StreamWriter("../../todayPoints.txt", false);
            sw.WriteLine(_dateToday.Day.ToString() + "." + _dateToday.Month.ToString() + "." + _dateToday.Year.ToString());
            foreach (Points p in _today)
            {
                line = p.CurrentPoints.ToString() + "," + p.Player;
                sw.WriteLine(line);
            }
            sw.Close();
        }

        public void loadScores() //Iz text fajla prebere podatke in upošteva datum
        {
            try
            {
                StreamReader sr = new StreamReader("../../hallOfFamePoints.txt");
                String line;
                String[] data = new String[2];
                Points dp;
                for (int i = 0; i < 5; i++)
                {
                    line = sr.ReadLine();
                    data = line.Split(',');
                    dp = new Points(data[1], Convert.ToInt32(data[0]));
                    _hallOfFame.Add(dp);
                }
                sr.Close();
                sr = new StreamReader("../../todayPoints.txt");
                String date = sr.ReadLine();
                if (checkDates(date))
                {
                    for (int i = 0; i < 5; i++)
                    {
                        line = sr.ReadLine();
                        data = line.Split(',');
                        dp = new Points(data[1], Convert.ToInt32(data[0]));
                        _today.Add(dp);
                    }
                }
                else
                {
                    for (int i = 0; i < 5; i++)
                    {
                        _today.Add(new Points("-", 0));
                    }
                }
            }
            catch (Exception e) { }
        }

        public static void prepareForScoreFill()
        {
            for (int i = 0; i < HallOfFame.Count; i++)
            {
                if (HallOfFame[i].CurrentPoints == 0)
                {
                    HallOfFame.RemoveAt(i);
                    i--;
                }
            }
            for (int i = 0; i < Today.Count; i++)
            {
                if (Today[i].CurrentPoints == 0)
                {
                    Today.RemoveAt(i);
                    i--;
                }
            }
        }

        public static void refill()
        {
            while (Today.Count < 6)
            {
                _today.Add(new Points("-", 0));
            }
            while (HallOfFame.Count < 6)
            {
                _hallOfFame.Add(new Points("-", 0));
            }
        }

        public void clearAll()
        {
            _today.Clear();
            _hallOfFame.Clear();
            for (int i = 0; i < 5; i++)
            {
                _today.Add(new Points("-", 0));
            }
            for (int i = 0; i < 5; i++)
            {
                _hallOfFame.Add(new Points("-", 0));
            }

            saveScores();
        }
    }
}
